package com.social.controller;

import com.mzt.logapi.starter.annotation.LogRecord;
import com.social.pojo.Result;
import com.social.utils.QiniuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @className: UploadFileController
 * @description: TODO 进行文件上传
 * @author: XiaoXu
 * @date: 2022/10/03 12:36
 * @Company: Copyright© [日期] by [作者或个人]
 **/
@RestController
@RequestMapping("/upload")
public class UploadFileController {

    @RequestMapping
    public Result upload(@RequestParam("file") MultipartFile imgFile) {
        //获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //截取文件后缀类型
        int index = originalFilename.lastIndexOf(".");
        String last = originalFilename.substring(index - 1);//获取文件后缀名
        String fileName = UUID.randomUUID().toString() + last;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail(201, "上传失败");
        }
        return Result.ok(200, fileName);
    }
}

